﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMMacroShape
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal sealed class CApCMMacroShape
{
  private const string MacroCircleRadius = "CM_MACRO_CIRCLE_RADIUS";
  private const string MacroRectangleLength = "CM_MACRO_RECTANGLE_LENGTH";
  private const string MacroRectangleWidth = "CM_MACRO_RECTANGLE_WIDTH";
  private const string MacroSlotLength = "CM_MACRO_SLOT_LENGTH";
  private const string MacroSlotRadius = "CM_MACRO_SLOT_RADIUS";
  private const string MacroPart = "CM_MACRO_PART";
  private List<double> macroParameters = new List<double>();
  private static Dictionary<CApCMShape, string[]> MacroParameters = new Dictionary<CApCMShape, string[]>()
  {
    {
      CApCMShape.Circle,
      new string[1]{ "CM_MACRO_CIRCLE_RADIUS" }
    },
    {
      CApCMShape.Rectangle,
      new string[2]
      {
        "CM_MACRO_RECTANGLE_LENGTH",
        "CM_MACRO_RECTANGLE_WIDTH"
      }
    },
    {
      CApCMShape.Slot,
      new string[2]
      {
        "CM_MACRO_SLOT_LENGTH",
        "CM_MACRO_SLOT_RADIUS"
      }
    }
  };

  public CApCMShape Shape { get; private set; }

  public ITxLocatableObject Part { get; set; }

  public double[] Parameters => this.macroParameters.ToArray();

  private CApCMMacroShape()
  {
  }

  private static ITxLocatableObject GetPartFromViaLocation(TxRoboticViaLocationOperation viaLocation)
  {
    if (TxRoboticViaLocationOperation.op_Equality(viaLocation, (ITxObject) null))
      return (ITxLocatableObject) null;
    return viaLocation.GetParameter("CM_MACRO_PART") is TxRoboticTxObjectParam parameter ? parameter.Value as ITxLocatableObject : (ITxLocatableObject) null;
  }

  public static bool ViaContainsMacro(TxRoboticViaLocationOperation viaLocation)
  {
    if (TxRoboticViaLocationOperation.op_Equality(viaLocation, (ITxObject) null))
      return false;
    foreach (KeyValuePair<CApCMShape, string[]> macroParameter in CApCMMacroShape.MacroParameters)
    {
      bool flag = true;
      foreach (string str in macroParameter.Value)
      {
        if (viaLocation.GetParameter(str) == null)
        {
          flag = false;
          break;
        }
      }
      if (flag)
        return true;
    }
    return false;
  }

  public static CApCMMacroShape Create(TxRoboticViaLocationOperation viaLocation)
  {
    if (TxRoboticViaLocationOperation.op_Equality(viaLocation, (ITxObject) null))
      return (CApCMMacroShape) null;
    List<double> doubleList = new List<double>();
    foreach (KeyValuePair<CApCMShape, string[]> macroParameter in CApCMMacroShape.MacroParameters)
    {
      bool flag = true;
      doubleList.Clear();
      foreach (string str in macroParameter.Value)
      {
        if (!(viaLocation.GetParameter(str) is TxRoboticDoubleParam parameter))
        {
          flag = false;
          break;
        }
        doubleList.Add(parameter.Value);
      }
      if (flag)
      {
        ITxLocatableObject partFromViaLocation = CApCMMacroShape.GetPartFromViaLocation(viaLocation);
        return CApCMMacroShape.Create(macroParameter.Key, partFromViaLocation, doubleList.ToArray());
      }
    }
    return (CApCMMacroShape) null;
  }

  public static CApCMMacroShape Create(
    CApCMShape shapeType,
    ITxLocatableObject part,
    params double[] macroParameters)
  {
    if (!CApCMMacroShape.MacroParameters.ContainsKey(shapeType) || CApCMMacroShape.MacroParameters[shapeType].Length > macroParameters.Length)
      return (CApCMMacroShape) null;
    return new CApCMMacroShape()
    {
      Shape = shapeType,
      Part = part,
      macroParameters = ((IEnumerable<double>) macroParameters).Take<double>(CApCMMacroShape.MacroParameters[shapeType].Length).ToList<double>()
    };
  }

  public void Store(TxRoboticViaLocationOperation viaLocation)
  {
    CApCMMacroShape.RemoveAllMacroShapeParameters(viaLocation);
    int num = 0;
    foreach (string str in CApCMMacroShape.MacroParameters[this.Shape])
      viaLocation.SetParameter((TxRoboticParam) new TxRoboticDoubleParam(str, this.macroParameters[num++]));
    if (this.Part != null)
      viaLocation.SetParameter((TxRoboticParam) new TxRoboticTxObjectParam("CM_MACRO_PART", (ITxObject) this.Part));
    CApCMMacroShape.SetProcessTypes(viaLocation, this.Shape);
  }

  public static void RemoveAllMacroShapeParameters(TxRoboticViaLocationOperation viaLocation)
  {
    foreach (KeyValuePair<CApCMShape, string[]> macroParameter in CApCMMacroShape.MacroParameters)
    {
      foreach (string str in macroParameter.Value)
      {
        if (viaLocation.GetParameter(str) is TxRoboticDoubleParam)
          viaLocation.RemoveParameter(str);
      }
    }
    if (viaLocation.GetParameter("CM_MACRO_PART") is TxRoboticTxObjectParam)
      viaLocation.RemoveParameter("CM_MACRO_PART");
    CApCMMacroShape.SetProcessTypes(viaLocation, CApCMShape.None);
  }

  private static void SetProcessTypes(TxRoboticViaLocationOperation viaLocation, CApCMShape shape)
  {
    try
    {
      if (viaLocation.ParentRoboticOperation.Robot == null || viaLocation.ParentRoboticOperation.Robot.Controller == null)
        return;
      TxRobot robot = viaLocation.ParentRoboticOperation.Robot as TxRobot;
      ITxOlpRobotControllerParametersHandler parametersHandlerInstance = new TxOlpControllerUtilities().InitAndGetRobotControllerParametersHandlerInstance((ITxOperation) viaLocation);
      if (parametersHandlerInstance == null)
        return;
      string str = "";
      switch (shape)
      {
        case CApCMShape.None:
          str = "";
          break;
        case CApCMShape.Circle:
          str = "CutCircle";
          break;
        case CApCMShape.Slot:
          str = "CutOblong";
          break;
        case CApCMShape.Rectangle:
          str = "CutSquare";
          break;
      }
      if (Array.IndexOf<string>(new TxOlpMotionCommandServices(robot).GetAllProcessTypes((ITxOperation) viaLocation), str) == -1)
        return;
      parametersHandlerInstance.OnComplexValueChanged("Process Type", str, (ITxOperation) viaLocation);
    }
    catch
    {
    }
  }
}
